package com.example.presentshopping.ui.fragment.special

import BaseRecyclerAdapter
import BaseRecyclerHolder
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.example.presentshopping.R
import com.example.presentshopping.config.base.BaseMvpFragment
import com.example.presentshopping.config.bean.SpecialOrderBean
import com.example.presentshopping.mvp.presenter.PublicPresenter
import com.example.presentshopping.net.http.HttpRequest
import com.example.presentshopping.net.http.HttpURL
import com.example.presentshopping.net.observer.TaskCallback
import com.example.presentshopping.ui.activity.GroupDetailsActivity
import com.example.presentshopping.ui.activity.OrderDetailsActivity
import com.example.presentshopping.ui.activity.TeamActivity
import com.example.presentshopping.utils.glide.GlideUtil
import com.example.presentshopping.utils.tool.GsonUtils
import com.yanzhenjie.recyclerview.SwipeRecyclerView
import kotlinx.android.synthetic.main.complete_fragment.*


class CompleteFragment(var sta :String ) : BaseMvpFragment() {

    var page = 1
    var specialBean: SpecialOrderBean? = null
    var Special_recy: SwipeRecyclerView? = null

    var baseRecyclerAdapter: BaseRecyclerAdapter<SpecialOrderBean.DataBean.ListBean>? = null

    private var queryInfoPresenter: PublicPresenter? = null

    override fun initView(view: View?, savedInstanceState: Bundle?) {
        queryInfoPresenter = PublicPresenter(context, false, "")
        queryInfoPresenter?.attachView(this)

        Special_recy = view?.findViewById(R.id.recycler_view)
        setAdapter()
        page=1
        OrderList(sta)

        // 使用默认的加载更多的View。
        Special_recy?.useDefaultLoadMore()
        Special_recy?.loadMoreFinish(false, true)
        Special_recy?.setLoadMoreListener {
            page++
            OrderList(sta)
        }

    }

    override fun onResume() {
        super.onResume()
    }
    fun setAdapter() {
        baseRecyclerAdapter = object : BaseRecyclerAdapter<SpecialOrderBean.DataBean.ListBean>(
            context,
            null,
            R.layout.iemt_special
        ) {
            override fun convert(
                holder: BaseRecyclerHolder?,
                data: SpecialOrderBean.DataBean.ListBean,
                position: Int,
                isScrolling: Boolean,
                selectedPosition: Int
            ) {
                val time = holder?.getView<TextView>(R.id.time_tv)
                val title = holder?.getView<TextView>(R.id.title_tv)
                val amount = holder?.getView<TextView>(R.id.amount_tv)
                val number = holder?.getView<TextView>(R.id.number_tv)
                val thumb = holder?.getView<ImageView>(R.id.thumb_iv)
                val statustv = holder?.getView<TextView>(R.id.status_tv)
                val tv_getSMSCode =holder?.getView<TextView>(R.id.tv_getSMSCode)
                val Order_tv =holder?.getView<TextView>(R.id.Order_tv)
                val order_details = holder?.getView<TextView>(R.id.tv_getSMSCode1)

                val status = data?.status
                val team_status =data?.team_status
                var tvstatus =""

                time?.text = data.create_time
                title?.text = data.title
                amount?.text = " ￥" + data.amount
                number?.text = "X" + data.number
                Order_tv?.text = "订单编号：" + data.id
                GlideUtil.load(context, data?.thumb, thumb)
                when(team_status){
                    1 -> {
                        tvstatus = "拼团中"
                        tv_getSMSCode?.visibility = View.GONE
                        order_details?.visibility = View.GONE
                    }
                    2 -> {
                        when (status) {
                            1 -> {
                                tvstatus = "未支付"
                                tv_getSMSCode?.text = "立即支付"
                                tv_getSMSCode?.visibility = View.VISIBLE
                                order_details?.visibility = View.GONE
                            }
                            2 -> {
                                tvstatus = "待发货 "
                                order_details?.visibility = View.VISIBLE
                                tv_getSMSCode?.visibility =View.VISIBLE
                            }
                            3 -> {

                                tvstatus ="待收货"
                                tv_getSMSCode?.text ="确认收货"
                                order_details?.visibility = View.VISIBLE

                            }
                            4 -> {
                                tvstatus = "已取消"
                                order_details?.visibility = View.GONE
                                tv_getSMSCode?.visibility =View.GONE
                            }

                            5 -> {
                                tvstatus = "拼团成功"
                                tv_getSMSCode?.text ="拼团详情"
                                tv_getSMSCode?.visibility = View.VISIBLE
                                order_details?.visibility = View.GONE

                            }

                            6 -> {
                                tvstatus = "拼团失败"
                                tv_getSMSCode?.text ="拼团详情"
                                tv_getSMSCode?.visibility = View.VISIBLE
                                order_details?.visibility = View.GONE
                            }

                            7 -> {
                                tv_getSMSCode?.text ="拼团详情"
                                order_details?.visibility = View.VISIBLE
                                tv_getSMSCode?.visibility = View.VISIBLE
                                tvstatus = "已完成"}
                        }
                    }
            }
                statustv?.text = tvstatus

                tv_getSMSCode?.setOnClickListener {

                    if (tv_getSMSCode?.text.contains("确认收货")){
                        Setstatus(data.id)
                    }else{
                        if(status==1){
                            val  Inttem = Intent(context, TeamActivity().javaClass)
                            Inttem.putExtra("order_id", data.id)
                            context?.startActivity(Inttem)

                        }else{
                            val  Inttem = Intent(context, GroupDetailsActivity().javaClass)
                            Inttem.putExtra("id", data.id)
                            context?.startActivity(Inttem)
                        }
                    }
                }
                order_details?.setOnClickListener {
                    val  Inttem = Intent(context, OrderDetailsActivity().javaClass)
                    Inttem.putExtra("id", data.id)
                    context?.startActivity(Inttem)
                }
            }

        }
        Special_recy?.adapter = baseRecyclerAdapter
    }

    fun OrderList(status: String) {
        queryInfoPresenter?.netWorkRequestPost(
            HttpURL.Req_getOrderList,
            HttpRequest.getOrderList(1, status, page, 10)
        )
    }

    override fun onSuccess(data: String?) {
        Special_recy?.loadMoreFinish(false, true)
        specialBean = GsonUtils.getGson().fromJson(data, SpecialOrderBean().javaClass)
        if (specialBean?.data?.list?.size!! >0){
            baseRecyclerAdapter?.setData(specialBean?.data?.list)
        }else{
            if (page == 1){

            }else{
                Special_recy?.loadMoreFinish(true, false)
            }

        }
        if (page == 1) {
            if (specialBean?.data?.list?.size ==0) {
                tvrelyou.visibility = View.VISIBLE
            }
        }
    }

    override fun onFailure(e: Throwable?, code: Int, msg: String?, isNetWorkError: Boolean) {

    }

    override fun onDestroyView() {
        queryInfoPresenter?.detachView()
        super.onDestroyView()
    }

    override val layoutId: Int
        get() = R.layout.complete_fragment

     fun Setstatus(id:String){
         queryInfoPresenter?.netWorkRequestPost(HttpURL.Req_finshOrder,HttpRequest.getGoodsDetail(id),object: TaskCallback{
             override fun onSuccess(data: String?) {
                 onResume()
             }

             override fun onFailure(
                 e: Throwable?,
                 code: Int,
                 msg: String?,
                 isNetWorkError: Boolean
             ) {

             }

         })

     }


}